﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Web.UI;
using StructureMap;
using StructureMap.Pipeline;

namespace Medianamik.UI.Web.MVP.View
{
    public abstract class UserControlModuleViewWithPresenter<T> : BaseUserControl where T : class
    {
        protected T Presenter;
        public event EventHandler Initialize;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Presenter = NewPresenter();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (Initialize != null)
                Initialize(null, e);
        }

        protected virtual T NewPresenter()
        {
            return ObjectFactory
                .GetInstance<T>(new ExplicitArguments(new Dictionary<string, object> { { "view", this } }));
        }

        public NameValueCollection QueryString
        {
            get { return Page.Request.QueryString; }
        }

        public void Alert(string resourceSet, string resourceKey)
        {
            Page.Alert(Resources(resourceSet, resourceKey));
        }

        public CultureInfo CurrentEditionCulture
        {
            get { return Page.CurrentEditionCulture; }
        }

        public CultureInfo CurrentUICulture
        {
            get { return Page.CurrentUICulture; }
        }
    }
}